import 'package:flutter/material.dart';
import 'package:kasax_demos2/entry/data_posts_test.dart';

class DrawerDemo extends StatelessWidget {
  const DrawerDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primaryColor: Colors.cyan),
      home: SampleDrawerDemo(),
    );
  }
}

class SampleDrawerDemo extends StatefulWidget {
  const SampleDrawerDemo({Key? key}) : super(key: key);

  @override
  _SampleDrawerDemoState createState() => _SampleDrawerDemoState();
}

class _SampleDrawerDemoState extends State<SampleDrawerDemo> {
  GlobalKey<ScaffoldState> _globalKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _globalKey,
      appBar: AppBar(
        actions: [
          Padding(
            padding: EdgeInsets.all(10),
            child: Icon(
              Icons.search,
              color: Colors.white,
            ),
          )
        ],
        elevation: 0.0,
        centerTitle: true,
        title: Text("Sample Drawer", style: TextStyle(color: Colors.white)),
      ),
      drawer: MyDrawer(_globalKey),
    );
  }
}

class MyDrawer extends StatelessWidget {
  GlobalKey<ScaffoldState> _globalKey = GlobalKey<ScaffoldState>();

  MyDrawer(this._globalKey, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          DrawerHeader(
              decoration: BoxDecoration(
                  color: Colors.yellow[400],
                  image: DecorationImage(
                      fit: BoxFit.cover,
                      image: NetworkImage(
                        PostsDatas.TEST_POSTS[0].imageUrl,
                      ))),
              child: Text(
                'Drawer Header',
                style: TextStyle(color: Colors.white, fontSize: 24),
              )),
          UserAccountsDrawerHeader(
            // 当前用户图片
            currentAccountPicture: CircleAvatar(
              backgroundImage: NetworkImage(PostsDatas.TEST_POSTS[5].imageUrl),
            ),
            otherAccountsPictures: [
              CircleAvatar(
                backgroundImage:
                    NetworkImage(PostsDatas.TEST_POSTS[2].imageUrl),
              ),
              CircleAvatar(
                backgroundImage:
                    NetworkImage(PostsDatas.TEST_POSTS[3].imageUrl),
              ),
              CircleAvatar(
                backgroundImage:
                    NetworkImage(PostsDatas.TEST_POSTS[4].imageUrl),
              ),
            ],
            accountName: Text(
              "Ning hao",
              style:
                  TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
            ),
            accountEmail: Text(
              "Ninghao@163.com",
              style: TextStyle(color: Colors.white),
            ),
            decoration: BoxDecoration(
                color: Colors.yellow[400],
                //顶部背景颜色或者图片
                image: DecorationImage(
                    fit: BoxFit.cover,
                    colorFilter: ColorFilter.mode(
                        Colors.yellow[400]!.withOpacity(0.6),
                        BlendMode.hardLight),
                    image: NetworkImage(
                      PostsDatas.TEST_POSTS[0].imageUrl,
                    ))),
          ),
          ListTile(
            leading: CircleAvatar(
              child: Icon(Icons.message),
            ),
            title: Text("Message"),
            onTap: () => closeDrawer(context),
          ),
          ListTile(
            leading: CircleAvatar(
              child: Icon(Icons.circle),
            ),
            title: Text("Profile"),
            onTap: () => closeDrawer(context),
          ),
          ListTile(
            leading: CircleAvatar(
              child: Icon(Icons.settings),
            ),
            title: Text("Settings"),
            onTap: () => closeDrawer(context),
          ),
        ],
      ),
    );
  }

  closeDrawer(context) {
    Navigator.of(context).pop();
  }
}
